require 'basecamp.rb'

class OfflineBasecampWrapper
  def initialize()
    @connected = false
  end
  
  def connect!(params)
    @projects = [mock_project("A", 1), mock_project("B", 2), mock_project("C", 3)]
    @connected = (  params[:url] == 'project.projectpath.com' and 
                    params[:username] == 'Joe' and 
                    (params[:password] == 'foo' or params[:password] == 'bark') )
  end
  
  def connected?
    @connected
  end
  
  def projects
    validate
    @projects
  end
  
  def milestones(project_id, find="all")
    validate
    project_name = project_name_from_id(project_id)
    
    if project_name == nil then
      raise "Invalid project_id number. No project exists with project_id = '#{project_id}'"
    end
    
    [ mock_milestone("Milestone 1 (#{project_name})", 1), 
      mock_milestone("Milestone 2 (#{project_name})", 2), 
      mock_milestone("Milestone 3 (#{project_name})", 3) ]
  end
  
  def lists(project_id, milestone_id)
    validate
    project_name = project_name_from_id(project_id)
    
    [ mock_todo_list( "Story 1 (#{project_name})", "This is a test story", milestone_id, 1),
      mock_todo_list( "Story 2 (#{project_name})", "This is a test story", milestone_id, 2),
      mock_todo_list( "Story 3 (#{project_name})", "This is a test story", milestone_id, 3) ]
  end
      
  private
  def validate
    unless connected?
      raise "You are not connected to basecamp."
    end
  end
  
  def mock_project(label, id)
    Basecamp::Record.new("project", {"id" => id, "name" => 'Project ' + label })
  end
  
  def mock_milestone(label, id)
    Basecamp::Record.new("milestone", {"id" => id, "title"=> label, "deadline" => Date.today } )
  end
  
  def mock_todo_list(name, description, milestone_id, id)
    Basecamp::Record.new("todo-list",
      {
        "id" => id,
        "name" => name,
        "description" => description,
        "milestone-id" => milestone_id,
        "todo-items" => [ Basecamp::Record.new("todo-item", { "content" => "todo item 1h", "completed" => false }),
                          Basecamp::Record.new("todo-item", { "content" => "todo item 2h", "completed" => false }),
                          Basecamp::Record.new("todo-item", { "content" => "todo item 3h", "completed" => false }) ]
      }
    )
  end
  
  def project_name_from_id(project_id)
    project_name = nil
    @projects.each do |p|
      project_name = p.name if p.id == project_id
    end
    project_name
  end
end